/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package admin.matrices;

import DBMS.DataBaseManager;
import admin.permisos.PermisosManager;
import clases.*;
import java.util.ArrayList;
import java.util.Iterator;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

/**
 *
 * @author andreth
 */
public class ModificarMatriz extends DispatchAction {

    /*
     * forward name="success" path=""
     */
    private static final String SUCCESS = "success";
    private static final String READY = "ready";
    private static final String PAGE = "page";
    private static final String FAILURE = "failure";
    private static final String ADDCOLUMNA = "addColumna";
    private static final String ADDFILA = "addFila";
    private static final String ELIMFILA = "elimFila";
    private static final String ELIMCOLUMNA = "elimColumna";
    private static final String name = "Modificar Matriz";

    public ActionForward populate(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
//        Usuario usr = (Usuario) request.getSession().getAttribute("logueado");
//        DataBaseManager dbms = DataBaseManager.getInstance();
//        if (!PermisosManager.validar(this.name, dbms.consultarPermisoAccion(name), usr.getPermiso())) {
//            System.out.println("No tiene permiso");
//            return mapping.findForward(FAILURE);
//        }
        DataBaseManager dbms = DataBaseManager.getInstance();
        Matriz m = (Matriz) form;
        Matriz matriz = DataBaseManager.getInstance().consultarMatriz((Matriz) form);
        System.out.println("matrizConsultada" + matriz.getNombre());
        for (Columna col : matriz.getColumnas()) {
            if ("Lista".equals(col.getTipo())) {
                System.out.println("El tipo de la columna es lista" + col.getIdLista());
                ArrayList<Lista> listas = DataBaseManager.getInstance().consultarListas();
                for (Lista lista : listas) {
                    if (lista.getIdLista() == (col.getIdLista())) {
                        col.setLista(lista);
                    }
                }

                System.out.println("\t  GUARDO LA LISTA " + col.getLista());
                request.getSession().setAttribute(col.getLista().getNombre(), col.getLista().getValores());
            }

        }
        request.getSession().setAttribute("columnas", matriz.getColumnas());
        request.getSession().setAttribute("Matriz", matriz);
        return mapping.findForward(READY);
    }

    public ActionForward prepopulate(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
//        Usuario usr = (Usuario) request.getSession().getAttribute("logueado");
//        DataBaseManager dbms = DataBaseManager.getInstance();
//        if (!PermisosManager.validar(this.name, dbms.consultarPermisoAccion(name), usr.getPermiso())) {
//            System.out.println("No tiene permiso");
//            return mapping.findForward(FAILURE);
//        }
        DataBaseManager dbms = DataBaseManager.getInstance();
        ArrayList<Matriz> matrices = DataBaseManager.getInstance().consultarMatrices();
        request.setAttribute("matrices", matrices);
        return mapping.findForward(PAGE);
    }

    public ActionForward saveNueva(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
//        Usuario usr = (Usuario) request.getSession().getAttribute("logueado");
//        DataBaseManager dbms = DataBaseManager.getInstance();
//        if (!PermisosManager.validar(this.name, dbms.consultarPermisoAccion(name), usr.getPermiso())) {
//            System.out.println("No tiene permiso");
//            return mapping.findForward(FAILURE);
//        }
        DataBaseManager dbms = DataBaseManager.getInstance();
        Matriz m = (Matriz) form;
        return mapping.findForward(SUCCESS);
    }

    public ActionForward eliminarFila(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
//        Usuario usr = (Usuario) request.getSession().getAttribute("logueado");
//        DataBaseManager dbms = DataBaseManager.getInstance();
//        if (!PermisosManager.validar(this.name, dbms.consultarPermisoAccion(name), usr.getPermiso())) {
//            System.out.println("No tiene permiso");
//            return mapping.findForward(FAILURE);
//        }
        DataBaseManager dbms = DataBaseManager.getInstance();
        System.out.println("paso por aqui");
        String link = request.getParameter("link");
        Matriz m = (Matriz) request.getSession().getAttribute("Matriz");
        m.setSubm(link);
        boolean success= DataBaseManager.getInstance().eliminarFilas(m);
        if(success){
            request.setAttribute("Matriz",m);
            return mapping.findForward(SUCCESS);
        }
        else{
            return mapping.findForward(FAILURE);
        }
    }

    public ActionForward save(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
//        Usuario usr = (Usuario) request.getSession().getAttribute("logueado");
//        DataBaseManager dbms = DataBaseManager.getInstance();
//        if (!PermisosManager.validar(this.name, dbms.consultarPermisoAccion(name), usr.getPermiso())) {
//            System.out.println("No tiene permiso");
//            return mapping.findForward(FAILURE);
//        }
        DataBaseManager dbms = DataBaseManager.getInstance();
        Matriz m = (Matriz) form;
        Matriz ma = (Matriz) request.getAttribute("Matriz");

        ArrayList<Columna> arrCol = (ArrayList) request.getSession().getAttribute("columnas");
        m.setColumnas(arrCol);
        boolean modificada = DataBaseManager.getInstance().modificarMatriz(m);
        if (modificada) {
            m = DataBaseManager.getInstance().consultarMatriz(m);
            m.setMensajeOK("Matriz Almacenada Satisfactoriamente");
            request.getSession().setAttribute("Matriz", m);
            return mapping.findForward(SUCCESS);
        } else {
            m.setMensaje("Error modificando su matriz");
            System.out.println("Error modificando su matriz");
            return mapping.findForward(FAILURE);
            
        }
    }

    public ActionForward addColumna(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
//        Usuario usr = (Usuario) request.getSession().getAttribute("logueado");
//        DataBaseManager dbms = DataBaseManager.getInstance();
//        if (!PermisosManager.validar(this.name, dbms.consultarPermisoAccion(name), usr.getPermiso())) {
//            System.out.println("No tiene permiso");
//            return mapping.findForward(FAILURE);
//        }
        DataBaseManager dbms = DataBaseManager.getInstance();
        Matriz m = (Matriz) form;
        return mapping.findForward(SUCCESS);
    }

    public ActionForward addFila(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
//        Usuario usr = (Usuario) request.getSession().getAttribute("logueado");
//        DataBaseManager dbms = DataBaseManager.getInstance();
//        if (!PermisosManager.validar(this.name, dbms.consultarPermisoAccion(name), usr.getPermiso())) {
//            System.out.println("No tiene permiso");
//            return mapping.findForward(FAILURE);
//        }
        DataBaseManager dbms = DataBaseManager.getInstance();
        Matriz m = (Matriz) request.getSession().getAttribute("Matriz");
        ArrayList<Columna> arrCol = (ArrayList) request.getSession().getAttribute("columnas");
        ArrayList<Celda> arrCel = new ArrayList();
        ArrayList<Celda> arc = null;
                    
        if(m.getFilas()==null){
            arc = new ArrayList<Celda>();
        }
        else{
            arc=m.getFilas();
        }
        for (Columna col : arrCol) {
            Celda cel = new Celda();

            cel.setIdColumna(col.getIdColumna());
            arrCel.add(cel);
            arc.add(cel);
        }
        Matriz mat = new Matriz();
        mat.setFilas(arrCel);
        m.setFilas(arc);
        mat.setIdMatriz(m.getIdMatriz());
        boolean b = DataBaseManager.getInstance().agregarFilas(mat);
        if (b) {
            request.setAttribute(SUCCESS, m);
            request.getSession().setAttribute("Matriz", m);
            request.setAttribute("Matriz", m);
            return mapping.findForward(ADDFILA);
        }


        return mapping.findForward(FAILURE);
    }

    public ActionForward elimColumna(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        Usuario usr = (Usuario) request.getSession().getAttribute("logueado");
        DataBaseManager dbms = DataBaseManager.getInstance();
        if (!PermisosManager.validar(this.name, dbms.consultarPermisoAccion(name), usr.getPermiso())) {
            System.out.println("No tiene permiso ");
            return mapping.findForward(FAILURE);
        }
        Matriz m = (Matriz) form;
        return mapping.findForward(SUCCESS);
    }

    public ActionForward elimFila(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        Usuario usr = (Usuario) request.getSession().getAttribute("logueado");
        DataBaseManager dbms = DataBaseManager.getInstance();
        if (!PermisosManager.validar(this.name, dbms.consultarPermisoAccion(name), usr.getPermiso())) {
            System.out.println("No tiene permiso ");
            return mapping.findForward(FAILURE);
        }
        Matriz m = (Matriz) form;
        String s = (String) request.getAttribute("indiceElim");
        request.getSession().setAttribute("Ruta", new Ruta("Accion: Modificando Matriz"));
        return mapping.findForward(SUCCESS);
    }
}
